package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.PushListAdapter;
import com.sanpu.kxe.business.main.bean.PushDetailsOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.PushListContract;
import com.sanpu.kxe.business.main.presenter.PushListPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;

import java.util.List;

import butterknife.Bind;

/**
 * Description：地推物料列表
 * Created by 薛勇军 on 2018/8/20.
 */

public class PushListActivity extends BaseTitleActivity implements PushListContract.View {

    @Bind(R.id.list_view)
    RecyclerView vListView;
//    @Bind(R.id.swipeRefreshLayout)
//    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.emptyView)
    View emptyView;

    private PushListAdapter mAdapter;

    private PushListContract.Presenter mPresenter;

    private int nowPage = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_push_list;
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new PushListAdapter();
        vListView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        vListView.setAdapter(mAdapter);
//        mAdapter.disableLoadMoreIfNotFullPage(vListView);
//        mAdapter.setPreLoadNumber(2);

        String materialName = getIntent().getStringExtra(MainConfig.INTENT_MATERIAL_NAME);
        if (!TextUtils.isEmpty(materialName)) {
            commonTitle.setTitle(materialName);
        }
        String materialType = getIntent().getStringExtra(MainConfig.INTENT_MATERIAL_TYPE);
        mPresenter = new PushListPresenter(this, materialType);
        mPresenter.getMaterialDetailsList(nowPage);
    }

    @Override
    protected void setListener() {
        super.setListener();
//        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                nowPage = 0;
//                mPresenter.getMaterialDetailsList(nowPage);
//                mAdapter.setEnableLoadMore(true);
//            }
//        });
//        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                mPresenter.getMaterialDetailsList(nowPage);
//            }
//        }, vListView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PushDetailsOutput detailsOutput = ((PushListAdapter)adapter).getItem(position);
                if (detailsOutput != null) {
                    MainOpenProvider.getInstance().openPushDetailActivity(mContext, detailsOutput.getTitle(),
                            detailsOutput.getPosterPath());
                }
            }
        });
    }

    @Override
    public void showPushListView(List<PushDetailsOutput> detailOutputs, boolean isFirst) {
        if (detailOutputs != null && detailOutputs.size()>0) {
            vListView.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            mAdapter.setNewData(detailOutputs);
//            if (isFirst) {
//            } else {
//                mAdapter.addData(detailOutputs);
//            }
//            nowPage++;
        } else {
            vListView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
//        if (vSwipeRefreshLayout.isRefreshing()) {
//            vSwipeRefreshLayout.setRefreshing(false);
//        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(PushListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
